package com.ruoyi.ipms_patent.rabbitmq;

import com.rabbitmq.client.Channel;
import com.ruoyi.common.core.constant.RabbitMQConstants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*邮箱消费者，实际上统一个微服务类消费比如（售后中心）然后发邮件给客户*/
@Component
@RabbitListener(queues = RabbitMQConstants.EMAIl_QUEUE)
public class EmailMQReceiver {

    @Autowired
    private JavaMailSender javaMailSender;

    //消费消息
    @RabbitHandler
    public void receiver(Email email,Channel channel, Message message) {
        //创建简单邮件消息
        SimpleMailMessage simpleEmail = new SimpleMailMessage();
        //发送
        simpleEmail.setFrom("willican@qq.com");
        //接收
        simpleEmail.setTo(email.getTo());
        //邮件时间
        simpleEmail.setSentDate(new Date());
        //邮件标题
        simpleEmail.setSubject("IPMS服务邮件");
        //邮件内容
        simpleEmail.setText(email.getMsg());
        try {
            //发送邮件
            javaMailSender.send(simpleEmail);

            //手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

            System.out.println("发送成功");
        } catch (MailException | IOException e) {
            e.printStackTrace();
            System.out.println("发送失败");
        }
    }
}